package com.biboheart.breport.dataset.data;

import com.biboheart.breport.core.BrContext;
import com.biboheart.breport.dataset.BrDataSet;
import com.biboheart.breport.dataset.cell.BrCell;
import com.biboheart.breport.dataset.cell.BrCellSimple;
import com.biboheart.breport.dataset.cell.BrCellSum;
import com.biboheart.breport.support.definition.FieldTypeEnum;
import com.biboheart.brick.utils.BitUtils;
import com.biboheart.brick.utils.MapUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class BrDataSetSimple implements BrDataSet {
    //private final List<Map<String, Object>> contentList = new ArrayList<>();
    private final Map<String, Map<String, BrCell>> cellHash = new HashMap<>();
    private final List<Map<String, BrCell>> cellList = new ArrayList<>();

    @Override
    public void addData(BrContext context, Map<String, Object> data) {
        String sn = getGroupSn(context, data);
        if (null == sn) {
            sn = MapUtils.getValue(data, "_index", String.class);
        }
        Map<String, BrCell> cellMap = cellHash.get(sn);
        if (null == cellMap) {
            cellMap = createCellMap(context);
            cellList.add(cellMap);
            cellHash.put(sn, cellMap);
        }
        for (Map.Entry<String, Object> entry : data.entrySet()) {
            String key = entry.getKey();
            Object value = entry.getValue();
            BrCell cell = cellMap.get(key);
            if (null == cell) {
                continue;
            }
            cell.addValue(value);
        }
    }

    @Override
    public List<Map<String, Object>> extract() {
        List<Map<String, Object>> contentList = new ArrayList<>();
        cellList.forEach(cellMap -> {
            Map<String, Object> content = new HashMap<>();
            cellMap.forEach((key, cell) -> content.put(key, cell.getValue()));
            contentList.add(content);
        });
        return contentList;
    }

    private Map<String, BrCell> createCellMap(BrContext context) {
        Map<String, BrCol> colHash = context.getColHash();
        Map<String, BrCell> cellMap = new HashMap<>();
        for (Map.Entry<String, BrCol> entry : colHash.entrySet()) {
            String key = entry.getKey();
            BrCol col = entry.getValue();
            if (BitUtils.isTrue(col.getType(), FieldTypeEnum.FIELD_TYPE_03.getType())) {
                cellMap.put(key, new BrCellSum());
            } else {
                cellMap.put(key, new BrCellSimple());
            }
        }
        return cellMap;
    }

    private String getGroupSn(BrContext context, Map<String, Object> data) {
        Map<String, BrCol> colHash = context.getColHash();
        StringBuilder snBuilder = new StringBuilder();
        for (Map.Entry<String, BrCol> entry : colHash.entrySet()) {
            String key = entry.getKey();
            BrCol col = entry.getValue();
            if (BitUtils.isTrue(col.getType(), FieldTypeEnum.FIELD_TYPE_04.getType())) {
                // 分组
                snBuilder.append("_").append(MapUtils.getValue(data, key, String.class));
            }
        }
        return snBuilder.length() > 0 ? snBuilder.substring(1) : null;
    }
}
